<template>
	<view class="page">
		<view class="body">
			<!-- 餐品详情 -->
			<view class="product_body">
				<!-- 标题 -->
				<view class="title">
					<text class="title_f">餐品详情：</text>
					<text class="title_b">{{deskId}}号桌</text>
				</view>
				<!-- 详情列表 -->
				<block v-for="food in cartList" :key="food.foodId">
					<view class="confirm_list">
						<text class="list_name">{{food.foodName}}</text>
						<text class="list_price">{{food.foodPrice.toFixed(1)}}, {{food.sellCount}}份</text>
					</view>
				</block>
				<!-- 小计 -->

				<view class="count">
					共{{totalNum}}件，小计：<text class="count_price">{{totalPrice}}</text>
				</view>

			</view>

			<!-- 用餐人数选择 -->
			<view class="mark_body">
				<!-- 标题 -->
				<view class="title p_title">请选择用餐人数：</view>
				<!-- 选择人数 -->
				<view class="peoples">
					<block v-for="num in peopleNumList" :key="num">
						<view :class="[numBoxClass, isActive==num?activeClass:'']" :data-num="num" @click="choosePeopleNum">{{num}}</view>
					</block>
				</view>
				<!-- 备注 -->
				<view class="title p_title">备注：</view>
				<view>
					<input class="mark_input" placeholder="请输入备注(例如:忌口,辣度等)" placeholder-style="font-size:30upx" :value="inputContent" @input="getInputText" />
				</view>

			</view>

		</view>
		<!-- 底部总计 -->
		<view class="footer_box">
			<view class="count_price footer_price">{{totalPrice}}</view>
			<view class="confirm_button" @click="confirmOrder">确认下单</view>
		</view>
	</view>
</template>

<script>
	import common from "../../common/common.js"
	export default {
		data() {
			return {
				deskId: 3,
				totalNum: 0,
				totalPrice: 0,
				cartList: [],
				peopleNumList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
				inputContent: '',
				peopleNum: 0,
				// orderVo: {},

				isActive: false,
				numBoxClass: 'people_num_box',
				activeClass: 'active_num_box'
			}
		},
		onLoad(option) {
			// 获取购物车信息
			// console.log(option.num)
			this.totalNum = option.num
			this.totalPrice = option.price
			var cartList = uni.getStorageSync('cart')
			this.cartList = cartList
			// 获取用餐人数和备注信息--加菜情况
			this.peopleNum=common.orderPeopleNum;
			this.isActive=this.peopleNum;
			this.inputContent=common.orderComment;
			
		},
		methods: {
			// 获取备注信息
			getInputText(event) {
				// console.log(event.target.value)
				this.inputContent = event.target.value
				// console.log(this.inputContent)
				common.orderComment=this.inputContent
			},
			// 选择用餐人数
			choosePeopleNum(e) {
				this.peopleNum = e.currentTarget.dataset.num;
				this.isActive = this.peopleNum;
				common.orderPeopleNum=this.peopleNum;
			},
			// 确认下单
			confirmOrder(e) {
				if (this.peopleNum == 0) {
					uni.showToast({
						title: '请选择用餐人数',
						icon: 'none'
					});
				} else {
					// var serverUrl=this.serverUrl;
					uni.showModal({
						title: '',
						content: '您确认下单吗？',
						showCancel: true,
						cancelText: '再看看',
						confirmText: '确定',
						success: res => {
							if (res.confirm) {
								console.log('确认下单了。。。')
								// console.log('用餐人数', this.peopleNum)
								// console.log('桌号', this.deskId)
								// console.log('订单list', this.cartList)
								// 获取必要数据
								var userInfo = uni.getStorageSync('wxUserInfo');
								// console.log('用户信息',userInfo)
								var orderId=uni.getStorageSync('orderId')||'';
								
								// 存放传输数据
								var orderVo = {}
								orderVo.orderId=orderId;
								orderVo.userInfo = userInfo;
								orderVo.orderCost = this.totalPrice;
								orderVo.orderDeskId = this.deskId;
								orderVo.orderPayStatus = 0;
								orderVo.orderPeoples = this.peopleNum;
								var cartListDto = []

								for (var i in this.cartList) {
									var cartItem = {}
									cartItem.itemCost = (this.cartList[i].sellCount * this.cartList[i].foodPrice).toFixed(2)
									cartItem.foodId = this.cartList[i].foodId;
									cartItem.sellCount = this.cartList[i].sellCount;
									cartListDto.push(cartItem);
								}
								orderVo.orderData = cartListDto;
								// console.log(orderVo)
								var orderVoJson=JSON.stringify(orderVo)
								// console.log('json type',orderVoJson)
								// 下单，传数据到后台，入库
								uni.request({
									url: this.serverUrl + '/wAddOrder',
									method: 'POST',
									data: orderVoJson,
									success: res => {
										// console.log(res)
										if (res.data.code == 1) {
											console.log("提交订单成功!")
											// 保存数据到缓存
											try {
												var orderList=uni.getStorageSync('orderList')||[];
												orderList=orderList.concat(this.cartList)
												// console.log(orderList)
												// 保存订单数据
												uni.setStorageSync('orderList', orderList)
												// 清空购物车数据
												uni.setStorageSync('cart', '')
												// 保存订单id
												uni.setStorageSync('orderId',res.data.dataString)
											} catch (e) {
												//TODO handle the exception
												console.log(e)
											}
											// 跳转页面
											uni.reLaunch({
												url:'../orderDetail/orderDetail?deskId='+this.deskId
											})
										} else {
											console.log("提交订单失败！")
										}
									},
									fail: () => {},
									complete: () => {}
								});
							}
						},
						fail: () => {},
						complete: () => {}
					});
				}

			}
		}
	}
</script>

<style>
	@import url("confirm.css");
</style>
